/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package network.packets;

import datamodel.Order;
import utils.StringUtils;
import utils.Utils;


/**
 *
 * @author Kvest
 */
public class OrderResponce extends Packet
{
    protected static final String WITHOUT_SUMM = "БезСуммы";
    protected static final String USUAL = "Обычный";
    
    protected Order order;
   
    public OrderResponce(int id)
    {
        super(id);
    }
    
    public OrderResponce(byte[] data)
    {
        super(ORDER_RESPONCE);
        order = new Order(-1);

        parse(data);
    }

    public void createOrder()
    {
        order = new Order(-1);
    }

    protected void parse(byte[] data)
    {
        int offset = 0;

        //Пропускаем название пакета
        byte[] buffer4 = new byte[4];
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);
        
        parse(data, offset);
    }
    
    public int parse(byte[] data, int offset)
    {
        byte[] buffer4 = new byte[4];
        
        //OrderID
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        order.setOrderID(Utils.byteToInt(buffer4));

        //RelayID
        //Не используется
        offset += 4;

        //Folder
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        int strSize = Utils.byteToInt(buffer4);
        byte[] buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        order.setFolder(StringUtils.bytesToStr(buffer));
       
        //Пропускаем пока   public bool NonCashPay;
        offset++;

        //ClientTypes
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);

        //Date
        offset += 8;
 
        //Fare
        buffer = new byte[16];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        order.setFare(Utils.byteToDecimal(buffer));

        //DispatcherName
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);
        
        //PhoneNumber
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);

        //StreetName
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);
        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        String adressTmp = StringUtils.bytesToStr(buffer) + ", ";

        //House
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);
        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        //Пропускаем пока res[9] = Utils.byteToString(temp);

        //AddressFact
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);

        //Flat
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);
        
        //Entrance
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);
        
        //Building
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);
        
        //Region
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);
        
        //BonusSum
        offset += 16;
        
        //GeoX & GeoY
        offset += 8;
       
        //SubOrders
        //SubOrders count
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        int count = Utils.byteToInt(buffer4);
        
        boolean hasSuborders = false;
        
        for (int j=0;j<count;j++)
        {
            //��������� �� 4 �����,�.�. ��� ���� ������ ���������.�� ��� �� �����.
            offset += 4;

            //Tariff   
            System.arraycopy(data, offset, buffer4, 0, buffer4.length);
            offset += buffer4.length;
            strSize = Utils.byteToInt(buffer4);
            buffer = new byte[strSize];
            System.arraycopy(data, offset, buffer, 0, buffer.length);
            offset += buffer.length;
            String tariff = StringUtils.bytesToStr(buffer);       
            if (tariff.equals(WITHOUT_SUMM))
            {
                order.setFare(-1);
            } else {
                hasSuborders = true;
            } 

            //From
            System.arraycopy(data, offset, buffer4, 0, buffer4.length);
            offset += buffer4.length + Utils.byteToInt(buffer4);
            //tempString += StringUtils.bytesToStr(buffer);                      

            //To
            System.arraycopy(data, offset, buffer4, 0, buffer4.length);
            offset += buffer4.length + Utils.byteToInt(buffer4);

            //��������� 4 ��������� ���� float ��� �� �����
            offset += 16;
        }

        order.setHasSuborders(hasSuborders);
        
        //OrderType
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        strSize = Utils.byteToInt(buffer4);
        buffer = new byte[strSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length; 
        order.setOrderType(StringUtils.bytesToStr(buffer));
    
        //Comments
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);

        //Пропускаем IsAdvanced
        offset++;

        //AutoTariffClassUID
        offset += 4;
        
        //PartnerPreffix
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length + Utils.byteToInt(buffer4);
                
        //изменяем значение текущего индекса(это параметры, которые идут в конце пакета, но я их не учитываю)
        offset += 8;

        buffer4 = null;
        buffer = null;
        
        return offset;
    }
   
    public Order getOrder()
    {
        return order;
    }
    
    protected String format(String value)
    {
        if (value == null || value.length() == 0)
        {
            return "";
        }

        String res = "";
        char ch,chn;
        for (int i = 0; i < (value.length() - 1); ++i)
        {
            ch = value.charAt(i);
            chn = value.charAt(i + 1);
            res += ch;
            if ((chn < 48 || chn > 57) && (chn != '/') && (ch >=48 && ch <= 57))
            {
                res += ' ';
            }
        }

        res += value.charAt(value.length() - 1);

        return res;
    }
}
